﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JaernCloud.Language.Compiler.Types {
    
    /// <summary>
    /// Helper class for working with INodeType
    /// </summary>
    public static class NodeTypeHelper {

        /// <summary>
        /// The path to the specified node type, starting at the root, down to and including the node type
        /// </summary>
        /// <param name="nodeType"></param>
        /// <returns></returns>
        public static List<INodeType> PathTo(this INodeType nodeType, Type excludeType = null) {
            List<INodeType> ret = new List<INodeType>();
            var current = nodeType;
            while (current != null) {
                if (excludeType != null && excludeType.IsAssignableFrom(current.GetType())) {
                    break;
                }
                ret.Add(current);
                current = current.Parent;
            }
            ret.Reverse();
            return ret;
        }

        public static TNodeType FindParent<TNodeType>(this INodeType nodeType) where TNodeType: INodeType {
            var parent = nodeType.Parent;
            while (parent != null) {
                if(typeof(TNodeType).IsAssignableFrom(parent.GetType())) {
                    return (TNodeType)parent;
                }
                parent = parent.Parent;
            }
            return default(TNodeType);
        }

        /// <summary>
        /// Traverses the specified node in a depth first order
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static IEnumerable<INodeType> DepthFirstTraversal(this INodeType context) {
            foreach (var child in context.Children) {
                yield return child;
                foreach (var descendant in DepthFirstTraversal(child)) {
                    yield return descendant;
                }
            }
        }

        /// <summary>
        /// Traverses the specified node in a depth first order
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static IEnumerable<INodeType> BreadthFirstTraversal(this INodeType context) {
            foreach (var child in context.Children) {
                yield return child;
            }
            foreach (var child in context.Children) {
                foreach (var descendant in BreadthFirstTraversal(child)) {
                    yield return descendant;
                }
            }
        }

        /// <summary>
        /// Returns node types that reference the specified types by inheritance or expressions
        /// </summary>
        public static IEnumerable<INodeType> FindReferencesTo(INodeType context, INodeType type, params Type[] allowedTypeInterfaces) {
            var nodeTypes = BreadthFirstTraversal(context).Where(n => allowedTypeInterfaces.Any(t => t.IsAssignableFrom(n.GetType())));
            foreach (var nodeType in nodeTypes) {
                
                bool references = false;

                if (nodeType.Equals(type)) {
                    //nodeType is the referenced type
                    yield return nodeType;
                    references = true;
                }                
                if (references) continue;
                
                foreach (var inherited in nodeType.InheritsFrom) {
                    if (inherited.Equals(type)) {
                        //nodeType inherites from the referenced type
                        yield return nodeType;
                        references = true;
                        break;
                    }
                }
                if (references) continue;

                if (nodeType is IExpressionNodeType) {
                    var expressionType = nodeType as IExpressionNodeType;
                    NodeIdentifierScope scope = new NodeIdentifierScope(expressionType);
                    INodeType expressionReturnType = scope.Lookup(expressionType.Expression);
                    if (expressionReturnType.Equals(type)) {
                        //expressionType has an expression that references the type
                        yield return expressionType;
                    }
                }

            }
        }


    }
}
